//
//  NSString+HJ.m
//  0105-新浪微博(01)
//
//  Created by 黄杰 on 15/1/7.
//  Copyright (c) 2015年 黄杰. All rights reserved.
//

#import "NSString+HJ.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (HJ)

- (NSString *)fileNameAppendString:(NSString *)str
{
    // 如果没有传入任何字符串
    if (str.length == 0) {
        return self;
    }
    
    // 1.文件拓展名
    NSString *extension = [self pathExtension];
    // 2.获得没有拓展名的文件名
    NSString *shortName = [self stringByDeletingPathExtension];
    // 3.拼接str
    NSString *dest = [shortName stringByAppendingString:str];
    // 4.拼接拓展名
    return [dest stringByAppendingPathExtension:extension];
}

+ (NSString *)stringFromTimestamp:(NSString *)timestamp formatter:(NSString *)formatter
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue]];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = formatter;
    return [dateFormatter stringFromDate:date];
}

#pragma mark 使用MD5加密字符串
- (NSString *)MD5
{
    const char *cStr = [self UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    
    CC_MD5(cStr, strlen(cStr), digest);
    
    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [result appendFormat:@"%02x", digest[i]];
    }
    
    return result;
}

/**
 *  邮箱验证 MODIFIED BY HELENSONG
 */
+ (BOOL)isValidateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

/**
 *  手机号码验证 MODIFIED BY HELENSONG
 */
+ (BOOL)isValidateMobile:(NSString *)mobile
{
    //手机号以13， 15，18开头，八个 \d 数字字符
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    //    NSLog(@"phoneTest is %@",phoneTest);
    return [phoneTest evaluateWithObject:mobile];
}

+ (NSString *) phonetic:(NSString*)sourceString
{
    NSMutableString *source = [sourceString mutableCopy];
    CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);
    return source;
}

+ (NSString *)notRounding:(float)price afterPoint:(int)position
{
    NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundUp scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    NSDecimalNumber *ouncesDecimal;
    NSDecimalNumber *roundedOunces;
    
    ouncesDecimal = [[NSDecimalNumber alloc] initWithFloat:price];
    roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
    
    return [NSString stringWithFormat:@"%@",roundedOunces];
}

// 判断是否为空
+ (NSString *)nullWithStr:(NSString *)str
{
    if ([str isEqual:[NSNull null]]) {
        return @"";
    } else if ([str isEqualToString:@""]) {
        return @"";
    } else if ([str isEqualToString:@"(null)"]) {
        return @"";
    } else if (str == nil) {
        return @"";
    } else {
        return str;
    }
}

+ (NSString *)zeroWithStr:(NSString *)str
{
    if ([str isEqual:[NSNull null]]) {
        return @"0";
    } else if ([str isEqualToString:@""]) {
        return @"0";
    } else if ([str isEqualToString:@"(null)"]) {
        return @"";
    } else if (str == nil) {
        return @"0";
    } else {
        return str;
    }
}

+ (NSString *)decimalNumberMutiplyWithString:(NSString *)multiplierValue andOtherString:(NSString *)multiplicandValue
{
    NSDecimalNumber *multiplierNumber = [NSDecimalNumber decimalNumberWithString:multiplierValue];
    NSDecimalNumber *multiplicandNumber = [NSDecimalNumber decimalNumberWithString:multiplicandValue];
    NSDecimalNumber *product = [multiplicandNumber decimalNumberByMultiplyingBy:multiplierNumber];
    return [product stringValue];
}

+ (NSString *)decimalNumberByAdding:(NSString *)multiplierValue andOtherString:(NSString *)multiplicandValue
{
    NSDecimalNumber *multiplierNumber = [NSDecimalNumber decimalNumberWithString:multiplierValue];
    NSDecimalNumber *multiplicandNumber = [NSDecimalNumber decimalNumberWithString:multiplicandValue];
    NSDecimalNumber *product = [multiplicandNumber decimalNumberByAdding:multiplierNumber];
    return [product stringValue];
}

+ (NSString *)decimalNumberBySubtracting:(NSString *)multiplierValue andOtherString:(NSString *)multiplicandValue
{
    NSDecimalNumber *multiplierNumber = [NSDecimalNumber decimalNumberWithString:multiplierValue];
    NSDecimalNumber *multiplicandNumber = [NSDecimalNumber decimalNumberWithString:multiplicandValue];
    NSDecimalNumber *product = [multiplierNumber decimalNumberBySubtracting:multiplicandNumber];
    return [product stringValue];
}

@end
